// components/segmentBarBox/segmentBarBox.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    sections:{
      "保密法律法规": ["保密法、保密实施条例及相关法律法规", "保密管理规定、办法", "政策解读", "书籍推荐"],
      "保密知识": ["保密防范技能", "保密常识", "保密管理知识"],
      "保密警示教育": ["先进事迹", "泄密案例"],
      "保密史": ["保密传统教育", "红色保密史、人物", "历代保密那些事"],
      "当前形势下的保密工作": [""]
    },
    fId: 0 //fatherBar的id，用于判断点击的是不是已激活的父bar
  },

  /**
   * 组件的方法列表
   */
  methods: {
    _selectF: function(e){
      console.log("mainSegmentBar.js _selectF\n",e)
      let t = Number(e.detail.currentId)
      if(t !== this.data.fId){//检测点击的bar是否已激活
        this.data.fId = t
        let keys = Object.keys(this.data.sections)
        let c = keys[t]
        let b = this.data.sections[c]
        let childBar = this.selectComponent("#child");
        childBar._setSegmentItems(b) //改变child的内容
        this.triggerEvent('selectF', {Items: b}, {})
      }
    },
    _selectC: function(e){
      console.log("mainSegmentBar.js _selectC\n",e) //是type为selectItem的那个
      this.triggerEvent('changeChildItem', {currentId: e.detail.currentId}, {})
    },
    _changeC: function(newIndex){
      console.log("mainSegmentBar.js _changeC newIndex = ",newIndex)
      this.selectComponent("#child")._changeIndex(newIndex)
    },
    _changeF: function(newIndex){
      if(typeof(newIndex) == 'number'){
        this.setData({fId: newIndex})
        his.selectComponent("#father")._changeIndex(newIndex)
      }else if(typeof(newIndex) == 'string'){
        let a = Object.keys(this.data.sections)
        for(let i = 0, l = a.length; i < l; i++){
          if(a[i] === newIndex){
            this.setData({fId: i})
            this.selectComponent("#father")._changeIndex(i)
            let childBar = this.selectComponent("#child");
            childBar._setSegmentItems(this.data.sections[newIndex]) //改变child的内容
            break
          }
        }
      }
    }
  },

  attached: function(e){
    //初始化设置father和child
    let keys = Object.keys(this.data.sections)
    let bar = this.selectComponent("#father")
    bar._setSegmentItems(keys)
    let bar2 = this.selectComponent("#child")
    let v = this.data.sections[keys[0]]
    bar2._setSegmentItems(v)
    this.triggerEvent('selectF', {Items: v}, {})
  }
})
